//配置云环境
const app = getApp()
wx.cloud.init({
    env: 'cloud1-8gbitjqz4bc93039',
})
const db = wx.cloud.database()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        s: {},
        images: [], //选择图片
        images_success: [], //上传云存储后的云存储地址数组
        images_success_size: 0, //图片上传成功的数量

    },
    async upDate() {
        var that = this
        const res = await wx.cloud.deleteFile({
            fileList: [
                that.data.s.pic
            ]
        })
        console.log(res);
        app.globalData.sList[0].pic = that.data.images_success[0]
        await db.collection("student")
            .where({
                _openid: app.globalData.sList[0]._openid
            })
            .update({
                data: {
                    pic: that.data.images_success[0]
                }
            })
    },
    upload() {
        let that = this;
        wx.chooseMedia({ //异步方法
            mediaType: ['image'], //文件类型
            count: 1, //最多选择图片数量
            sizeType: ['original', 'compressed'], //选择的图片尺寸 原图，压缩图
            sourceType: ['album', 'camera'], //相册选图，相机拍照
            success(res) {
                //const tempFilePaths = res.tempFilePaths
                that.setData({
                    images: res.tempFiles
                });
                console.log("选择成功", res)
            }
        })
    },

    uploadImage(index) {
        let that = this
        wx.cloud.uploadFile({ //上传至微信云存储
            cloudPath: 'myImage/' + new Date().getTime() + "_" + Math.floor(Math.random() * 1000) + ".jpg", //使用时间戳加随机数给图片命名
            filePath: that.data.images[index].tempFilePath, // 本地文件路径
            success: res => {
                // 返回文件 ID
                console.log("上传成功", res.fileID)
                that.data.images_success[index] = res.fileID;
                that.data.images_success_size = that.data.images_success_size + 1;

                if (that.data.images_success_size == that.data.images.length) {
                    console.log("上传成功：", that.data.images_success)
                } else {
                    that.uploadImage(index + 1)
                }
                that.upDate()
                wx.hideLoading({
                    success: (res) => {
                        wx.navigateBack({})
                    },
                })
            },
            fail: err => {
                that.setData({
                    images_success: [],
                    images_success_size: 0
                })
                wx.showToast({
                    icon: 'none',
                    title: '上传失败，请重新上传',
                })
            }
        })
    },

    //提交表单添加到数据库
    addBtn: function (e) {
        wx.showLoading({
            title: '上传中',
        })
        let that = this;
        if (that.data.images.length > 0) { //1、判断是否有图片
            that.setData({
                //3、给上传图片初始化一个长度，上传成功的数组和已有的数组一致
                images_success: that.data.images
            })
            that.uploadImage(0) //2、有图片时先上传第一张
        }
    },
    pass() {
        wx.showModal({
            content: '确定要授权您的健康码信息吗',
            showCancel: true,
            title: '授权',
            complete: (res) => {
                if (res.confirm) {
                    app.globalData.sList[0].isAuthorized = true
                    db.collection("student")
                        .where({
                            _openid: app.globalData.sList[0]._openid
                        })
                        .update({
                            data: {
                                isAuthorized: true
                            }
                        }).then(res => {
                            wx.navigateBack({})
                        })
                }
            }
        })
    },
    reject() {
        wx.showModal({
            content: '确定要取消授权您的健康码信息吗',
            showCancel: true,
            title: '取消授权',
            complete: (res) => {
                if (res.confirm) {
                    app.globalData.sList[0].isAuthorized = false
                    db.collection("student")
                        .where({
                            _openid: app.globalData.sList[0]._openid
                        })
                        .update({
                            data: {
                                isAuthorized: false
                            }
                        }).then(res => {
                            wx.navigateBack({})
                        })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        this.setData({
            s: app.globalData.sList[0]
        })
        console.log(this.data.s)
        if (this.data.time == true) {
            that.setData({
                t: "有"
            })
        } else {
            that.setData({
                t: "无"
            })
        }
        if (this.data.s.isAuthorized == true)
            this.setData({
                isAuthorized: "已授权"
            })
        else
            this.setData({
                isAuthorized: "未授权"
            })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})